package tenant

import (
	"context"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/base"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/base/profit"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/base/tenant"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils/ossfile"
	gfyxUserService "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service/tenant"
	"git.myscrm.cn/golang/stark/v4"
)

type TenantService struct {
	//orgRepo org.OrgRepositoryIface
	profitRepo profit.ProfitRepositoryIface
	tenantRepo tenant.TenantRepositoryIface
}

// 构造函数
func NewTenantService(
	profitRepo profit.ProfitRepositoryIface,
	tenantRepo tenant.TenantRepositoryIface,
) TenantServiceIface {
	return &TenantService{
		profitRepo: profitRepo,
		tenantRepo: tenantRepo,
	}
}

// 获取租户权益
func (t *TenantService) Profit(ctx context.Context) ([]string, error) {
	list, err := t.profitRepo.GetTenantProfit(ctx)
	if err != nil {
		return nil, err
	}

	return list, nil
}

func (t *TenantService) GetTenantInfo(ctx context.Context) (map[string]interface{}, error) {
	var (
		// 市场洞察 数量
		marketReportAmount int
		// 线索雷达-全国版 数量
		clueRadarNationwideAmount int
		// 线索雷达-省版 数量
		clueRadarProvinceAmount int
		// logo地址
		logoUrl string
	)
	// 获取数据权益
	profit, err := t.profitRepo.GetProfitProvince(ctx)
	if err != nil {
		return nil, err
	}
	profitProvince := profit.GetProvinceCodeList()
	if profitProvince == nil {
		profitProvince = make([]string, 0)
	}
	if profit.GetProfits() != nil {
		for _, item := range profit.GetProfits() {
			if item.ProductCode == "ClueRadarProvince" {
				clueRadarProvinceAmount = clueRadarProvinceAmount + int(item.Number)
			} else if item.ProductCode == "ClueRadarNationwide" {
				clueRadarNationwideAmount++
			} else if item.ProductCode == "MarketReport" {
				marketReportAmount++
			}
		}
	}
	// 获取网站配置信息
	siteInfo, err := t.profitRepo.GetSiteInfo(ctx)
	if err != nil {
		return nil, err
	}
	// 获取logo地址
	if siteInfo.GetLogo() != "" {
		logoUrl, err = ossfile.GetSignURL(siteInfo.GetLogo())
		if err != nil {
			return nil, err
		}
	}

	data := map[string]interface{}{
		"market_report_amount":         marketReportAmount,
		"clue_radar_nationwide_amount": clueRadarNationwideAmount,
		"clue_radar_province_amount":   clueRadarProvinceAmount,
		"province_code_list":           profitProvince,
		"logo_url":                     logoUrl,
		"logo_path":                    siteInfo.GetLogo(),
	}
	return data, nil
}

func (t *TenantService) TenantSetInfo(ctx context.Context, logoFilePath string, provinceCodeList []string) error {
	// 保存省份数据权益
	if err := t.profitRepo.SaveProfitProvince(ctx, provinceCodeList); err != nil {
		return err
	}

	// 保存logo
	if err := t.profitRepo.SaveLogoFilePath(ctx, logoFilePath); err != nil {
		return err
	}

	return nil
}

func (t *TenantService) GetTenantList(ctx context.Context, request *base.TenantListRequest) (*base.TenantListResponse, error) {
	params := &gfyxUserService.GetTenantListRequest{
		Page:     request.Page,
		PageSize: request.PageSize,
	}
	tenantList, err := t.tenantRepo.GetTenantList(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTenantList failed, err:%v", err)
		return nil, err
	}
	response := &base.TenantListResponse{
		Total: tenantList.GetTotal(),
		List:  make([]*base.TenantListItem, 0),
	}
	for _, item := range tenantList.GetList() {
		response.List = append(response.List, &base.TenantListItem{
			TenantCode: item.GetTenantCode(),
			TenantName: item.GetTenantName(),
		})
	}
	return response, nil
}
